#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>

using namespace std;


struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};
class Solution {
public:
    ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) {
        int count1 = 0, count2 = 0;
        ListNode* cur1 = pHead1;
        ListNode* cur2 = pHead2;

        while (cur1) {
            cur1 = cur1->next;
            count1++;
        }
        while (cur2) {
            cur2 = cur2->next;
            count2++;
        }

        if (count1 > count2) {
            int count = count1 - count2;
            while (count--) {
                pHead1 = pHead1->next;
            }
        }
        else {
            int count = count2 - count1;
            while (count--) {
                pHead2 = pHead2->next;
            }
        }

        while (pHead1 != pHead2) {
            pHead1 = pHead1->next;
            pHead2 = pHead2->next;
        }
        return pHead1;
    }
};
//
//
//int main()
//{
//    string ret, s;
//    getline(cin, ret);
//    getline(cin, s);
//
//    int arr[520] = { 0 };
//
//    for (auto& ch : s)
//    {
//        arr[ch] = 1;
//    }
//
//    for (auto& ch : ret)
//    {
//        if (arr[ch] == 0)
//        {
//            cout << ch;
//        }
//    }
//    return 0;
//}